<!--
  - Copyright (c) 2018.
  - 北京九思金信科技有限公司对该文件内容保留所有权利。
  - 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
  -->

<template>
  <div>
    <el-row>
      <el-col :span="4" align="left" style="line-height: 36px;height: 36px">
        <span style="font-size: 14px;color: #747778">联系人信息</span>
      </el-col>
    </el-row>
    <el-row style="justify-content: center">
      <el-button @click="addRow()"
                 style="margin: 8px 0px 8px 0px;padding: 6.2px 7px;font-size: 13px;line-height: 18px"
                 v-show="tableData.length === 0 && canEdit && editable" round>
        <i class="joys-icon-new" style="margin-right: 12px;margin-left: 4px"/>
        <span style="margin: 0px">添加联系人信息</span>
      </el-button>
      <el-button type="primary"
                 @click="addRow()"
                 style="padding: 0px 18px 0px 8px;line-height: 36px;margin: 12px 0px"
                 v-show="editable && canEdit && !showForm && tableData.length > 0" round>
        <i class="joys-icon-addagain" style="margin-right: 18px;"/>
        <span style="margin: 0px;display: inline-block;font-size: 14px;">继续添加</span>
      </el-button>
      <span v-if="tableData.length === 0 && !editable">没有数据</span>
    </el-row>

    <transition name="el-zoom-in-top">
      <el-form :model="formData"
               ref="formData"
               :rules="rules"
               v-loading="loading"
               style="text-align: left;padding-top: 28px"
               :class="{cardBackColor:!canEdit}"
               v-show="showForm" inline>
        <el-row style="padding:0px 12px 24px 12px">
          <el-col :md="8" :lg="6">
            <el-form-item label="姓名" prop="linkmanName">
              <ft-input :editable="!canEdit" v-model="formData.linkmanName"
                        placeholder="请输入内容"></ft-input>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="部门" prop="linkmanDept">
              <ft-input :editable="!canEdit" v-model="formData.linkmanDept"
                        placeholder="请输入内容"></ft-input>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="职务" prop="linkmanJob">
              <ft-data-enum-select
                v-model="formData.linkmanJob"
                type-name="postLevel"
                placeholder="请选职务"
                :editing="!canEdit">
              </ft-data-enum-select>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="手机" prop="linkmanMobile">
              <ft-input :editable="!canEdit" v-model="formData.linkmanMobile"
                        placeholder="请输入内容" type="phonenum"></ft-input>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="邮箱" prop="linkmanMail">
              <ft-input :editable="!canEdit" v-model="formData.linkmanMail"
                        placeholder="请输入内容"></ft-input>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="办公电话" prop="linkmanTel">
              <ft-input :editable="!canEdit" v-model="formData.linkmanTel"
                        placeholder="请输入内容" type="phonenum"></ft-input>
            </el-form-item>
          </el-col>
        </el-row>

        <hr style="background-color: #EDEDED;border: none;height: 1px;margin: 0px"/>

        <el-row style="padding:16px 0px">
          <el-col align="right">
            <el-button size="mini"
                       @click="handleCancel()"
                       v-show="!canEdit && editable">取消
            </el-button>
            <el-button size="mini"
                       type="primary"
                       @click="handleSave()"
                       style="margin-left: 24px;margin-right: 36px"
                       v-show="!canEdit && editable">保存
            </el-button>
          </el-col>
          <el-col align="right" v-show="canEdit">
            <el-button size="mini"
                       style="margin-left: 24px;margin-right: 36px"
                       @click="handleCancel()">返回
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </transition>

    <transition name="el-zoom-in-top">
      <el-table :data="tableData"
                v-show="tableData.length > 0 && !showForm"
                tooltip-effect="light">
        <el-table-column prop="linkmanName" label="姓名" align="left" show-overflow-tooltip>
          <template slot-scope="scope">
            <ft-link-text @click="handleCard(scope.$index,scope.row)"
                          :text="scope.row.linkmanName">
            </ft-link-text>
          </template>
        </el-table-column>
        <el-table-column prop="linkmanDept" label="部门" align="left" show-overflow-tooltip></el-table-column>
        <el-table-column prop="linkmanJob" label="职务" align="left" show-overflow-tooltip>
          <template slot-scope="scope">
              <ft-data-enum-select
                          v-model="scope.row.linkmanJob"
                          type-name="postLevel"
                          placeholder="请选职务"
                          :editing="false">
              </ft-data-enum-select>
          </template>
        </el-table-column>
        <el-table-column prop="linkmanMobile" label="手机" align="left" show-overflow-tooltip inline></el-table-column>
        <el-table-column prop="linkmanMail" label="邮箱" align="left" show-overflow-tooltip inline></el-table-column>
        <el-table-column prop="linkmanTel" label="办公电话" align="left" show-overflow-tooltip inline></el-table-column>
        <el-table-column v-if="editable" label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
            <el-popover title="确认" placement="top" width="200" v-model="scope.row.delVisible">
              <p>数据删除后，将不能恢复，确认删除？</p>
              <div style="text-align:right; margin:0">
                <el-button size="small"
                           @click="scope.row.delVisible=false">取消
                </el-button>
                <el-button type="danger" size="small" @click="handleDelete(scope.$index, tableData)">确定</el-button>
              </div>
              <span slot="reference" style="margin-left:.10rem">
                  <el-button type="text"
                             size="small"
                             style="color:#B94A48">删除</el-button>
              </span>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </transition>
  </div>
</template>

<script>

  import {mobileFormat, mailFormat} from "@/pages/pc/assets/js/validate/rule.js"

  export default {
    name: "Linkman",
    components: {},
    props: {
      customerId: {
        type : String,
        default : '',
      },
      editable: {
        type : Boolean,
        default : true,
      },
    },
    mounted() {
      this.load()
    },
    data() {
      let validateMobile = (rule, value, callback) => {
        if (value != '' && value != null && !mobileFormat(value)) {
          callback(new Error("请输入正确的手机号码"))
        } else {
          callback()
        }
      }
      let validateMail = (rule, value, callback) => {
        if (value != '' && value != null && !mailFormat(value)) {
          callback(new Error("请输入正确的邮箱地址"))
        } else {
          callback()
        }
      }
      return {
        tableData: [],
        kmId: "",
        formData: {},
        dataIndex: 0,
        loading:false,
        showForm: {
          type: Boolean,
          default: false
        },
        canEdit: true,
        rules: {
          linkmanName: [
            {max: 80, message: '不能超过80字', trigger: 'blur' },
            {required: true,message: '联系人姓名不能为空',trigger: 'blur'}],
          linkmanDept:[
            {max: 80,message:'不能超过80字'}
          ],
          linkmanMobile: [
            {max: 11, message: '不能超过11字', trigger: 'blur'},
            {validator: validateMobile, trigger: 'blur'}],
          linkmanMail: [
            {max: 60, message: '不能超过60字', trigger: 'blur' },
            {validator: validateMail, trigger: 'blur',
          }],
          linkmanTel: [
            {max: 25, message: '不能超过25字', trigger: 'blur' }
          ],

        },
      }

    },
    methods: {
      data() {
        return {}
      },
      load() {
        this.$http.get('/joys-rest/lss-customer/custInfo/linkman/' + this.customerId).then(resp => {
          this.tableData = resp.body
          this.showForm = false
          // this.$emit('returnValue', {isShow3 : this.isShow})
        }, resp => {
          this.$message.error(resp.body)
        })
      },
      //列表编辑按钮
      handleEdit(index, row) {
        this.dataIndex=index
        this.formData = JSON.parse(JSON.stringify(row))
        this.canEdit = false
        this.showForm = true
      },
      //列表删除按钮
      handleDelete(index, row) {
        this.kmId = row[index].linkmanId
        this.$http.delete('/joys-rest/lss-customer/custInfo/linkman/' + this.kmId).then(
          resp => {
            row.splice(index, 1)
            this.$message.success('删除成功!')
          },
          resp => {
            this.$message.error(resp.body)
        })
      },
      //列表取消
      handleCancel() {
        this.tableData[this.dataIndex]=this.formData
        this.showForm = false
        this.canEdit = true
        this.$refs.formData.clearValidate()
      },
      //列表增行
      addRow() {
        this.formData = {
          sourceBillId: this.customerId,
          sourceBillType: "lss_linkman",
        }
        this.dataIndex = this.tableData.length + 1 ;
        this.canEdit = false
        this.showForm = true
      },
      //保存
      handleSave() {
        this.loading=true
        this.$refs.formData.validate((valid) => {
          if (valid) {
            this.$http.post('/joys-rest/lss-customer/custInfo/linkman/save/', this.formData).then(
              resp => {
                this.tableData.splice(this.dataIndex, 1, resp.body)
                this.canEdit = true
                this.showForm = false
                this.loading=false
                this.$message.success('保存成功!')
              },
              resp => {
                this.loading=false
                this.$message.error(resp.body)
                this.canEdit = false
            })
          }
          else {
            this.loading=false
            return false
          }
        })
      },
      //列表卡片按钮
      handleCard(index, row) {
        this.formData = row;
        this.canEdit = true;
        this.showForm = true
      },
      //字数较多时，超出部分用...显示
      convertInputValue(val) {
        if (null != val && '' != val && val.length > 9) {
          return val.substr(0, 9) + '...'
        }
        return val
      },
    },
  }
</script>
<style>
  .cardBackColor {
    background-color: #F4F8F9;
    margin-bottom: 4px;
  }

  .tableClass .cell {
    word-break: keep-all;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>
<style scoped>
</style>
